net.sourceforge.simplegamenet.util.proportionlayout
Class ProportionLayout

java.lang.Object
  extended bynet.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2, Serializable

public class ProportionLayout
extends Object
implements LayoutManager2, Serializable

The ProportionLayout class is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size.

The ProportionLayout class defines a grid wherein components can be laid out. Each component managed by a ProportionLayout is associated with an instance of ProportionConstraints. The ProportionConstraints object specifies where a component's display area should be located on the grid and how the component should be positioned within its display area. In addition to its constraints object, the ProportionLayout also considers each component's minimum and preferred sizes in order to determine a component's size.

The following figures show six components (all buttons) managed by a proportion layout. Figure 1 shows the layout for the components with their preferred size and Figure 2 shows the layout for the same components resized to a larger size.

Figure 1: the example with its preferred size
Figure 2: the example resized to a larger size

In this example there are 3 columns and 3 rows. The proportion of column 1 is set to NO_PROPORTION and the proportion of columns 2 and 3 are set to 1.0 The proportion of row 1 and 3 are set to NO_PROPORTION and the proportion of row 2 is set to 1.0 The rows and columns with proportion set to NO_PROPORTION means that they have a fixed size that will always stay the same, even after resizing. Those with proportion set to 1.0 get all the extra size to divide among them when the layout is resized to a larger size.

Here is the code that implements the example shown above:


 import java.awt.*;
 import javax.swing.*;
 

import net.sourceforge.simplegamenet.util.proportionlayout.*;

public class LayoutTester extends JFrame {

public static void main(String[] args) { LayoutTester currentFrame = new LayoutTester(); currentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); currentFrame.pack(); currentFrame.show(); }

public LayoutTester() { super("LayoutTester"); Container contentPanel = getContentPane(); ProportionLayout layout = new ProportionLayout();

// First add all the columns to the ProportionLayout layout.appendColumn(10); // column 0 // Column 0 will be an empty space of width 10 layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // column 1 // Column 1 will always be the greatest preferred width of all it's components // Column 1 will never get any of the additional width layout.appendColumn(10); // column 2 // Column 2 will be an empty space of width 10 layout.appendColumn(0, 1.0); // column 3 // Column 3 will always be the same width as column 5 // Column 3 and 5 will devide any of the additional width between them layout.appendColumn(10); // column 4 // Column 4 will be an empty space of width 10 layout.appendColumn(0, 1.0); // column 5 // Column 5 will always be the same width as column 3 // Column 3 and 5 will devide any of the additional width between them layout.appendColumn(10); // column 6 // Column 6 will be an empty space of width 10

// Then add all the rows to the ProportionLayout layout.appendRow(10); // row 0 // Row 0 will be an empty space of width 10 layout.appendRow(0, ProportionLayout.NO_PROPORTION); // row 1 // Row 1 will always be the greatest preferred height of all it's components // Row 1 will never get any of the additional height layout.appendRow(10); // row 2 // Row 2 will be an empty space of width 10 layout.appendRow(0, ProportionLayout.NO_PROPORTION); // row 3 // Row 3 will always be the greatest preferred height of all it's components // Row 3 will never get any of the additional height layout.appendRow(10); // row 4 // Row 4 will be an empty space of width 10 layout.appendRow(0, 1.0); // row 5 // Row 5 will get all of the additional height layout.appendRow(10); // row 6 // Row 6 will be an empty space of width 10

contentPanel.setLayout(layout); contentPanel.add(new JButton("B1"), new ProportionConstraints(1, 1) ); // column 1, row 1 contentPanel.add(new JButton("Button 2 takes up a lot of space"), new ProportionConstraints(3, 1) ); // column 3, row 1 contentPanel.add(new JButton("Button 3"), new ProportionConstraints(5, 1) ); // column 5, row 1 contentPanel.add(new JButton("B4"), new ProportionConstraints(1, 3, 3, 1) ); // column 1, row 3 with width 3, height 1 contentPanel.add(new JButton("Button 5"), new ProportionConstraints(1, 5) ); // column 1, row 5 contentPanel.add(new JButton("B6"), new ProportionConstraints(5, 3, 1, 3) ); // column 5, row 3 with width 1, height 3

// ProportionConstaints has additional constructors with more parameters // that allow a more flexible control of how the component will behave // inside it's own cell. }

}


Author:
Geoffrey and Jeroen
See Also:
Serialized Form

Field Summary
protected  TreeSet columnLineParts
          Contains all the columnLineParts
protected  ArrayList columnLines
          Contains all the columnLines for this ProportionLayout.
protected  double columnProportionTotal
          Contains the total amount of columnLines for this ProportionLayout.
protected  HashMap linePartCouples
          Contains which rowLine and columnLine belong together.
static double NO_PROPORTION
          Make the column or row have it's preferred width or height, without stretching.
protected  TreeSet rowLineParts
          Contains all the rowLineParts
protected  ArrayList rowLines
          Contains all the rowLines for this ProportionLayout.
protected  double rowProportionTotal
          Contains the total amount of rowLines for this ProportionLayout.
 
Constructor Summary
ProportionLayout()
          Creates a new instance of ProportionLayout
 
Method Summary
 void addLayoutComponent(Component component, Object object)
          Adds a component to the layout, using the specified ProportionConstraints of that object.
 void addLayoutComponent(String name, Component component)
          This method isn't supported.
 int appendColumn()
          Creates a new column at the end of the grid for this ProportionLayout.
 int appendColumn(int minimumWidth)
          Creates a new column at the end of the grid for this ProportionLayout.
 int appendColumn(int minimumWidth, double lineProportion)
          Creates a new column at the end of the grid for this ProportionLayout.
 int appendRow()
          Creates a new row at the end of the grid for this ProportionLayout.
 int appendRow(int minimumHeight)
          Creates a new row at the end of the grid for this ProportionLayout.
 int appendRow(int minimumHeight, double lineProportion)
          Creates a new row at the end of the grid for this ProportionLayout.
protected  Dimension arrangeGrid(Container parent, int sizeType)
          Arranges the grid for the given container of components and specifies the sizeType of it.
 float getLayoutAlignmentX(Container parent)
          Returns the alignment along the x axis.
 float getLayoutAlignmentY(Container parent)
          Returns the alignment along the y axis.
 void invalidateLayout(Container parent)
          Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
 void layoutContainer(Container parent)
          Lays out the specified container using this proportion layout.
 Dimension maximumLayoutSize(Container parent)
          Returns the maximum dimensions for this layout given the components in the specified target container.
 Dimension minimumLayoutSize(Container parent)
          Determines the minimum size of the target container using this proportion layout.
 Dimension preferredLayoutSize(Container parent)
          Determines the preferred size of the target container using this proportion layout.
 void removeLayoutComponent(Component component)
          Removes a component from the ProportionLayout.
 String toString()
          Returns a string representation of this proportion layout's values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NO_PROPORTION

public static final double NO_PROPORTION
Make the column or row have it's preferred width or height, without stretching.

See Also:
Constant Field Values

columnLines

protected ArrayList columnLines
Contains all the columnLines for this ProportionLayout.


rowLines

protected ArrayList rowLines
Contains all the rowLines for this ProportionLayout.


columnProportionTotal

protected double columnProportionTotal
Contains the total amount of columnLines for this ProportionLayout.


rowProportionTotal

protected double rowProportionTotal
Contains the total amount of rowLines for this ProportionLayout.


linePartCouples

protected HashMap linePartCouples
Contains which rowLine and columnLine belong together.


columnLineParts

protected TreeSet columnLineParts
Contains all the columnLineParts


rowLineParts

protected TreeSet rowLineParts
Contains all the rowLineParts

Constructor Detail

ProportionLayout

public ProportionLayout()
Creates a new instance of ProportionLayout

Method Detail

appendColumn

public int appendColumn()
                 throws IllegalArgumentException
Creates a new column at the end of the grid for this ProportionLayout. The minimunWidth is 0 and the lineProportion is NO_PROPORTION.

Returns:
the gridX value for the new column.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

appendColumn

public int appendColumn(int minimumWidth)
                 throws IllegalArgumentException
Creates a new column at the end of the grid for this ProportionLayout. The lineProportion is NO_PROPORTION.

Parameters:
minimumWidth - Spicifies the minimum width for this column.
Returns:
the gridX value for the new column.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

appendColumn

public int appendColumn(int minimumWidth,
                        double lineProportion)
                 throws IllegalArgumentException
Creates a new column at the end of the grid for this ProportionLayout.

Parameters:
minimumWidth - specifies the minimum width for this column.
lineProportion - specifies the proportion for this column.
Returns:
the gridX value for the new column.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

appendRow

public int appendRow()
              throws IllegalArgumentException
Creates a new row at the end of the grid for this ProportionLayout. The minimunHeight is 0 and the lineProportion is NO_PROPORTION.

Returns:
the gridY value for the new row.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

appendRow

public int appendRow(int minimumHeight)
              throws IllegalArgumentException
Creates a new row at the end of the grid for this ProportionLayout. The lineProportion is NO_PROPORTION.

Parameters:
minimumHeight - specifies the minimum height for this row.
Returns:
the gridY value for the new row.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

appendRow

public int appendRow(int minimumHeight,
                     double lineProportion)
              throws IllegalArgumentException
Creates a new row at the end of the grid for this ProportionLayout.

Parameters:
minimumHeight - specifies the minimum height for this row.
lineProportion - specifies the proportion for this row.
Returns:
the gridY value for the new row.
Throws:
IllegalArgumentException - if one of the arguments is an illegal argument.

arrangeGrid

protected Dimension arrangeGrid(Container parent,
                                int sizeType)
Arranges the grid for the given container of components and specifies the sizeType of it.

Parameters:
parent - specified the container to be laid out.
sizeType - specifies the sizeType for this container.
Returns:
the arranged grid for this specific container of components.

addLayoutComponent

public void addLayoutComponent(Component component,
                               Object object)
                        throws IllegalArgumentException,
                               IndexOutOfBoundsException
Adds a component to the layout, using the specified ProportionConstraints of that object.

Specified by:
addLayoutComponent in interface LayoutManager2
Parameters:
component - specifies the component to lay out.
object - specifies the cell in which the component is to be laid out.
Throws:
IllegalArgumentException - if the object used as parameter is not of the ProportionConstraints type.
IndexOutOfBoundsException - if the index of the object in which you are putting the component doesn't exist.

addLayoutComponent

public void addLayoutComponent(String name,
                               Component component)
This method isn't supported.

Specified by:
addLayoutComponent in interface LayoutManager
Parameters:
name - the name of the component.
component - the component to be added.

removeLayoutComponent

public void removeLayoutComponent(Component component)
Removes a component from the ProportionLayout.

Specified by:
removeLayoutComponent in interface LayoutManager
Parameters:
component - specifies which component has to be removed.

minimumLayoutSize

public Dimension minimumLayoutSize(Container parent)
Determines the minimum size of the target container using this proportion layout.

Specified by:
minimumLayoutSize in interface LayoutManager
Parameters:
parent - the component to be laid out
Returns:
the laid out component.

preferredLayoutSize

public Dimension preferredLayoutSize(Container parent)
Determines the preferred size of the target container using this proportion layout.

Specified by:
preferredLayoutSize in interface LayoutManager
Parameters:
parent - the container to be laid out.
Returns:
the laid out container

maximumLayoutSize

public Dimension maximumLayoutSize(Container parent)
Returns the maximum dimensions for this layout given the components in the specified target container.

Specified by:
maximumLayoutSize in interface LayoutManager2
Parameters:
parent - the component which needs to be laid out.
Returns:
the laid out component.

getLayoutAlignmentX

public float getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentX in interface LayoutManager2
Parameters:
parent - the component/container which needs to be aligned along the x axis.
Returns:
the component/container aligned along the x axis.

getLayoutAlignmentY

public float getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis. This specifies how the component would like to be aligned relative to other components. The value should be a number between 0 and 1 where 0 represents alignment along the origin, 1 is aligned the furthest away from the origin, 0.5 is centered, etc.

Specified by:
getLayoutAlignmentY in interface LayoutManager2
Parameters:
parent - the component/container which needs to be aligned along the y axis.
Returns:
the component/container aligned along the y axis.

invalidateLayout

public void invalidateLayout(Container parent)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.

Specified by:
invalidateLayout in interface LayoutManager2
Parameters:
parent - the container that needs to be invalidated.

layoutContainer

public void layoutContainer(Container parent)
Lays out the specified container using this proportion layout. This method reshapes components in the specified container in order to satisfy the constraints of this ProportionLayout object.

Specified by:
layoutContainer in interface LayoutManager
Parameters:
parent - the container in which to do the layout.

toString

public String toString()
Returns a string representation of this proportion layout's values.

Returns:
a string representation of this proportion layout.


Copyright © 2003-2004 The 3 Belgians. All Rights Reserved.